package com.magic.zhixin.controller;

import com.magic.zhixin.controller.requestEntity.QuestionReqEntity;
import com.magic.zhixin.entity.*;
import com.magic.zhixin.service.ZhiXinWenDaService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@CrossOrigin
@RequestMapping("/api/zhixinwenda/")
public class ZhiXinWenDaController extends BaseController {

    @Autowired
    private ZhiXinWenDaService zhiXinWenDaService;

    @GetMapping("addQuestionBank")
    @ApiOperation(value = "自定义题库")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "name", value = "", required = true, dataType = "String"),
    })
    public Result<QuestionBank> addQuestionBank(@RequestParam(name = "name") String name) {
        UserInfo userInfo = currentUser();
        return zhiXinWenDaService.addQuestionBank(name,userInfo);
    }

    @PostMapping("addQuestion")
    @ApiOperation(value = "自定义问题")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "questionTitle", value = "问题", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "answers", value = "问题答案", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "questionBankId", value = "题库id", dataType = "long"),
    })
    public Result<QuestionBank> addQuestion(@RequestBody @Validated  QuestionReqEntity questionReqEntity) {
        UserInfo userInfo = currentUser();

        return zhiXinWenDaService.addQuestion(  questionReqEntity.getQuestionTitle(),questionReqEntity.getAnswers(), questionReqEntity.getQuestionBankId(),userInfo);
    }


    @GetMapping("getQuestionBank")
    @ApiOperation(value = "获取题库")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "type", value = "类型1：我的 2：系统", required = true, dataType = "int"),
    })
    public Result<QuestionBank> getQuestionBank(Integer type) {
        UserInfo userInfo = currentUser();
        return zhiXinWenDaService.getQuestionBank(type,userInfo);
    }

    @GetMapping("getRandomQuestion")
    @ApiOperation(value = "获取题库中随机一个问题")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "questionBankId", value = "题库id", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "questionId", value = "原题id，换题时必传", required = false, dataType = "Long"),
    })
    public Result getRandomQuestion(Long questionBankId, String oldQuestionIds) {
        return zhiXinWenDaService.getRandomQuestion(questionBankId, oldQuestionIds);
    }
    @GetMapping("getNextQuestion")
    @ApiOperation(value = "获取题库中随机一个问题")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "questionBankId", value = "题库id", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "questionId", value = "原题id，换题时必传", required = false, dataType = "Long"),
    })
    public Result getNextQuestion(Long questionBankId, Long questionId) {
        return zhiXinWenDaService.getNextQuestion(questionBankId, questionId);
    }

    @GetMapping("addUserQuestion")
    @ApiOperation(value = "用户提问")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "questionBankId", value = "题库id", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "questionId", value = "原题id，换题时必传", required = false, dataType = "Long"),
    })
    public Result addUserQuestion(Long questionBankId, Integer totalAmount, String questionIds, String rightIds,
                                  Integer questionType, BigDecimal totalBonus,BigDecimal fee, Integer rewardAmount,
                                  Integer rightAmount) {
        UserInfo userInfo = currentUser();
        try {
            return zhiXinWenDaService.addUserQuestion(questionBankId, totalAmount, questionIds, rightIds, questionType, totalBonus, fee,rewardAmount, rightAmount,userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.FailedResult(e.getMessage());
        }
    }

    @GetMapping("getMyQuestionRecord")
    @ApiOperation(value = "我出的题")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "questionBankId", value = "题库id", required = true, dataType = "Long"),
    })
    public Result<UserQuestion> getMyQuestionRecord() {
        UserInfo userInfo = currentUser();
        return zhiXinWenDaService.getMyQuestion(userInfo);
    }

    @GetMapping("getUserQuestionDetail")
    @ApiOperation(value = "题目详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "questionBankId", value = "题库id", required = true, dataType = "Long"),
    })
    public Result<UserQuestion> getUserQuestionDetail(Long userQuestionId) {
        return zhiXinWenDaService.getUserQuestionDetail(userQuestionId);
    }

    @GetMapping("getUserQuestionAnswerDetail")
    @ApiOperation(value = "查看别人答题情况")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "questionBankId", value = "题库id", required = true, dataType = "Long"),
    })
    public Result<UserQuestionAnswerDetail> getUserQuestionAnswerDetail(Long userQuestionAnswerId) {
        return zhiXinWenDaService.getUserQuestionAnswerDetail(userQuestionAnswerId);
    }

    @GetMapping("getRightAnswer")
    @ApiOperation(value = "获取正确答案")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "questionBankId", value = "题库id", required = true, dataType = "Long"),
    })
    public Result<UserQuestion> getRightAnswer(Long userQuestionId) {
        UserInfo userInfo = currentUser();
        return zhiXinWenDaService.getRightAnswer(userQuestionId,userInfo);
    }


    @GetMapping("getMyAnswerRecord")
    @ApiOperation(value = "我答的题")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "questionBankId", value = "题库id", required = true, dataType = "Long"),
    })
    public Result<UserQuestionAnswer> getMyAnswerRecord() {
        UserInfo userInfo = currentUser();
        return zhiXinWenDaService.getMyAnswerRecord(userInfo);
    }

    @GetMapping("getAllUserQuestionAnswer")
    @ApiOperation(value = "知心问答")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "questionBankId", value = "题库id", required = true, dataType = "Long"),
    })
    public Result<UserQuestionAnswer> getAllUserQuestionAnswer(Long userQuestionAnswerId) {
        return zhiXinWenDaService.getAllUserQuestionAnswer(userQuestionAnswerId);
    }

    @GetMapping("getMyUserQuestionAnswer")
    @ApiOperation(value = "获取我的答题情况")
    @ApiImplicitParams({
    })
    public Result<UserQuestionAnswer> getMyUserQuestionAnswer(Long userQuestionId) {
        return zhiXinWenDaService.getMyUserQuestionAnswer(currentUser(),userQuestionId);
    }

    @GetMapping("addUserQuestionAnswer")
    @ApiOperation(value = "用户回答")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "questionBankId", value = "题库id", required = true, dataType = "Long"),
    })
    public Result<UserQuestion> addUserQuestionAnswer(Long userQuestionId, String questionIds, String answerIds) {
        try {
            return zhiXinWenDaService.addUserQuestionAnswer(userQuestionId, questionIds, answerIds,currentUser());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.FailedResult(e.getMessage());
        }
    }

    @GetMapping("getCopywriting")
    @ApiOperation(value = "获取文案")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "type", value = "0:普通文案 1 红包文案", required = true, dataType = "int"),
    })
    public Result<Copywriting> getCopywriting(Integer type) {
        return zhiXinWenDaService.getCopywriting(type);
    }

    @GetMapping("getQuestionList")
    @ApiOperation(value = "获取问题列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userQuestionId", value = "", required = true, dataType = "int"),
    })
    public Result<UserQuestionDetail> getQuestionList(Long userQuestionId) {
        return zhiXinWenDaService.getQuestionList(userQuestionId);
    }
    @GetMapping("getDebugType")
    @ApiOperation(value = "是否是debug 模式")

    public Result getDebugType() {
        return Result.OkResult(1);
    }

}
